package com.csgc.camera.widget.dialog

import android.content.Context
import android.view.MotionEvent
import android.widget.ImageView
import android.widget.TextView
import com.csgc.camera.constants.Constants.getChangeColor
import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.util.etFilter.RemarkFilter
import com.csgc.camera.widget.dialog.base.BottomDialog
import com.cysd.spsyxj.R
import com.hjq.shape.view.ShapeEditText
import com.hjq.shape.view.ShapeTextView
import com.hjq.toast.ToastUtils

class EditImageTxtDialog(val context: Context) {

    private lateinit var mDialog: BottomDialog
    private var inputText: ShapeEditText?=null
    private var colorView1: ImageView?=null
    private var colorView2: ImageView?=null
    private var colorView3: ImageView?=null
    private var colorView4: ImageView?=null
    private var colorView5: ImageView?=null
    private var colorView6: ImageView?=null
    private var colorView7: ImageView?=null

    private var labelSure: TextView?=null
    private var modelStyle1: ShapeTextView?=null
    private var modelStyle2: ShapeTextView?=null

    private var modelStyle3: ShapeTextView?=null
    private var modelStyle4: ShapeTextView?=null

    private var ivClose: ImageView?=null

    private var selectorColor = 0
    private var selectStyle = 0

    var listener:OnLabelCheckListener?=null

    init {
        initDialog()
    }
    private fun initDialog(){
        mDialog = BottomDialog(context).apply {
            setCustomView(R.layout.dialog_image_txt)
            inputText = findViewById(R.id.et_input_text)
            labelSure = findViewById(R.id.tv_sure_label)
            ivClose = findViewById(R.id.iv_close)

            inputText?.filters = arrayOf(RemarkFilter(30))

            colorView1 = findViewById(R.id.sv_color_1)
            colorView2 = findViewById(R.id.sv_color_2)
            colorView3 = findViewById(R.id.sv_color_3)
            colorView4 = findViewById(R.id.sv_color_4)
            colorView5 = findViewById(R.id.sv_color_5)
            colorView6 = findViewById(R.id.sv_color_6)
            colorView7 = findViewById(R.id.sv_color_7)


            modelStyle1 = findViewById(R.id.layout_style_1)
            modelStyle2 = findViewById(R.id.layout_style_2)
            modelStyle3 = findViewById(R.id.layout_style_3)
            modelStyle4 = findViewById(R.id.layout_style_4)

            initEvents()

            colorView2?.postDelayed(
                Runnable {
                    changeColor(2)
                }, 1000
            )
        }



    }

    fun show(){
        inputText?.setText("")
        mDialog.show()
    }


    private fun initEvents(){

        ivClose?.clickWithTrigger {
            mDialog.dismiss()
        }

        labelSure?.clickWithTrigger {

            val text = inputText?.text?.toString()?.trim()
            if (text.isNullOrEmpty()){
                ToastUtils.show("请输入文字")
            }else if (selectStyle == 0){
                ToastUtils.show("请选择模版")
            }else{
                listener?.onCheck(selectStyle,getChangeColor(context,selectorColor),text)
            }
            mDialog.dismiss()
        }

        modelStyle1?.clickWithTrigger {
            changeStyle(1)
        }
        modelStyle2?.clickWithTrigger {
            changeStyle(2)
        }
        modelStyle3?.clickWithTrigger {
            changeStyle(3)
        }
        modelStyle4?.clickWithTrigger {
            changeStyle(4)
        }


        inputText?.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_UP) {
                inputText?.compoundDrawables?.get(2)?.let { drawableRight ->
                    inputText?.let { editText ->
                        if (event.rawX >= (editText.right - drawableRight.bounds.width())) {
                            editText.setText("")
                            return@setOnTouchListener true
                        }
                    }
                }
            }
            false
        }


        colorView1?.clickWithTrigger {changeColor(1) }
        colorView2?.clickWithTrigger { changeColor(2) }
        colorView3?.clickWithTrigger {changeColor(3)  }
        colorView4?.clickWithTrigger {changeColor(4)  }
        colorView5?.clickWithTrigger {changeColor(5)  }
        colorView6?.clickWithTrigger {changeColor(6)  }
        colorView7?.clickWithTrigger {changeColor(7)  }

    }

    private fun changeColor(active:Int){
        selectorColor = active
        colorView1?.isSelected = active == 1
        colorView2?.isSelected = active == 2
        colorView3?.isSelected = active == 3
        colorView4?.isSelected = active == 4
        colorView5?.isSelected = active == 5
        colorView6?.isSelected = active == 6
        colorView7?.isSelected = active == 7
    }

    interface OnLabelCheckListener{
        fun onCheck(style:Int,color:Int,text:String)
    }

    private fun changeStyle(style:Int){
        selectStyle = style
        val text = inputText?.text?.toString()?.trim()
        if (text.isNullOrEmpty()){
            ToastUtils.show("请输入文字")
            return
        }else if (selectStyle == 0){
            ToastUtils.show("请选择模版")
            return
        }else{
            listener?.onCheck(selectStyle,getChangeColor(context,selectorColor),text)
        }
        mDialog.dismiss()
    }




}